﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.ProcureManage
{
    public partial class InvoiceManage : BasePage
    {
        public InvoiceManage()
        {
            this.ModuleNo = Module.单据管理.ToString();
            this.PageOperateType = OperateType.发票管理.ToString();
        }
        IBLL_Purchase iPurchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                QueryResult();
            }
        }
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            QueryResult();
        }
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void QueryResult()
        {
            IList<Model.View.Invoice> LisInvoice = new List<Model.View.Invoice>();
            Model.Query.Invoice qryCondition = new Model.Query.Invoice();
            qryCondition.InvoiceTime = new HZ.Common.ConvertHelper(searchInvoiceTime.Text.ToString().Trim()).ToDateTime;
            qryCondition.InvoiceID = searchInvoiceID.Text.ToString().Trim();
            qryCondition.CompanyID_PS = searchCompanyID_PS.Text.ToString().Trim();
            qryCondition.HospitalName = searchHospitalName.Text.ToString().Trim();
            switch (ddlAuditStatus.SelectedItem.Value)
            {
                //0未审核；1通过；2不通过
                //改为 0 未提交，1 已提交
                case "":
                    qryCondition.AuditStatus = null;
                    break;
                case "0":
                    qryCondition.AuditStatus = 0;
                    break;
                case "1":
                    qryCondition.AuditStatus = 1;
                    break;
            }
            qryCondition.SortFields = " InvoiceTime desc ";
            int totalCount = 0;
            IBLL_Purchase iPurchase = Factory.BLL_Purchase();
            LisInvoice = iPurchase.GetInvoiceByWhere(qryCondition, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
            AspNetPager1.RecordCount = totalCount;
            gvwInvoiceList.DataSource = LisInvoice;
            gvwInvoiceList.DataBind();
        }

        protected void gvwInvoiceList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblAuditStatus = e.Row.FindControl("lblAuditStatus") as Label;
                Label lblMod = e.Row.FindControl("lblMod") as Label;
                LinkButton lbnDel = e.Row.FindControl("lbtnDel") as LinkButton;
                if (lblAuditStatus.Text == "0")
                {
                    lblAuditStatus.Text = "未提交";
                }
                else if (lblAuditStatus.Text == "1")
                {
                    lblAuditStatus.Text = "已提交";
                }
            }
        }

        protected void gvwInvoiceList_SelectedIndexChanged(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            QueryResult();
        }

        protected void gvwInvoiceList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvwInvoiceList.EditIndex = -1;
            QueryResult();
        }

        protected void gvwInvoiceList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvwInvoiceList.EditIndex = e.NewEditIndex;
            QueryResult();
        }

        protected void gvwInvoiceList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Page.IsValid)
            {
                TextBox txtInvoiceNo = (TextBox)gvwInvoiceList.Rows[e.RowIndex].FindControl("txtInvoiceNo");
                TextBox txtInvoiceTime = (TextBox)gvwInvoiceList.Rows[e.RowIndex].FindControl("txtInvoiceTime");
                Label lblCompanyID_PS=(Label)gvwInvoiceList.Rows[e.RowIndex].FindControl("lblCompanyID_PS");
                Label lblHospitalname = (Label)gvwInvoiceList.Rows[e.RowIndex].FindControl("lblHospitalname");
                Label lblTotalAmount = (Label)gvwInvoiceList.Rows[e.RowIndex].FindControl("lblTotalAmount");
                if (string.IsNullOrEmpty(txtInvoiceNo.Text.Trim()))
                {
                    HZ.Web.WebUtil.Alert("发票号不能为空！", this);
                    ((TextBox)gvwInvoiceList.Rows[e.RowIndex].FindControl("txtInvoiceNo")).Focus();
                    return;
                }
                if (String.IsNullOrEmpty(txtInvoiceTime.Text.Trim()))
                {
                    HZ.Web.WebUtil.Alert("结算时间不能为空！", this);
                    ((TextBox)gvwInvoiceList.Rows[e.RowIndex].FindControl("txtInvoiceTime")).Focus();
                    return;
                }
                Invoice invoice = new Invoice();
                int nSuccess = 0;
                invoice.InvoiceSerialID = gvwInvoiceList.DataKeys[e.RowIndex].Values[0].ToString();
                invoice.InvoiceID = txtInvoiceNo.Text.Trim();
                invoice.InvoiceTime = Convert.ToDateTime(txtInvoiceTime.Text.Trim());
                invoice.Altertime = DateTime.Now;
                invoice.CompanyID_PS = lblCompanyID_PS.Text.Trim();
                invoice.HospitalID = lblHospitalname.Text.ToString().Trim();
                invoice.Note = "Altered by " + CurrentAccount.GetUser().Org.OrgName;
                invoice.TotalAmount = Convert.ToDecimal(lblTotalAmount.Text.ToString().Trim());
                invoice.TotalAmount_old = Convert.ToDecimal(lblTotalAmount.Text.ToString().Trim());
                invoice.Flag = 1;
                nSuccess = iPurchase.UpdateInvoice(invoice);
                if (nSuccess == 1)
                {
                    HZ.Web.WebUtil.Alert("修改成功!", this);
                    gvwInvoiceList.EditIndex = -1;
                    QueryResult();
                }
                else
                {
                    HZ.Web.WebUtil.Alert("修改失败!", this);
                    return;
                }
            }
        }

        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            QueryResult();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            Model.Query.Invoice qryCondition = new Model.Query.Invoice();
            qryCondition.InvoiceTime = new HZ.Common.ConvertHelper(searchInvoiceTime.Text.ToString().Trim()).ToDateTime;
            qryCondition.InvoiceID = searchInvoiceID.Text.ToString().Trim();
            qryCondition.CompanyID_PS = searchCompanyID_PS.Text.ToString().Trim();
            qryCondition.HospitalName = searchHospitalName.Text.ToString().Trim();
            switch (ddlAuditStatus.SelectedItem.Value)
            {
                //0未审核；1通过；2不通过
                //改为 0 未提交，1 已提交
                case "":
                    qryCondition.AuditStatus = null;
                    break;
                case "0":
                    qryCondition.AuditStatus = 0;
                    break;
                case "1":
                    qryCondition.AuditStatus = 1;
                    break;
            }
            qryCondition.SortFields = " InvoiceTime desc ";
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.Invoice>(qryCondition, true);
            string sql = string.Format(@"SELECT InvoiceID 发票号,Hospitalname 采购医疗机构,Companyname_PS 配送企业,TotalAmount 发票总金额,
                                       InvoiceTime 发票时间,case AuditStatus when 0 then '未提交' when 1 then '已提交' end as 确认状态 FROM View_Invoice WHERE 1=1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "发票信息.xls");
        }
    }
}
